package com.controller;/*
 *
 *作者：张继成
 *任务：心理老师
 *
 *备注：后端接口
 *@Date2023/06/11/12:32
 ***/

import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.XinlilaoshiEntity;
import com.entity.view.XinlilaoshiView;
import com.service.TokenService;
import com.service.XinlilaoshiService;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.apache.poi.hssf.eventmodel.ERFListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.parser.Entity;
import javax.xml.crypto.Data;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/xinlilaoshi")
public class XinlilaoshiController {
    @Autowired
    private XinlilaoshiService xinlilaoshiService;

    @Autowired
    private TokenService tokenService;

    /*
    * 登录
    * */
    @IgnoreAuth
    @RequestMapping(value="/login")
    public R login(String username, String password, String captcha, HttpServletRequest request){
        XinlilaoshiEntity u=xinlilaoshiService.selectOne(new EntityWrapper<XinlilaoshiEntity>().eq("laoshizhanghao",username));
        if(u==null || !u.getMima().equals(password)){
            return R.error("账号或密码不正确");
        }
        if(!"是".equals(u.getSfsh()))
            return R.error("账号已锁定，请联系管理员进行审核");
        String token=tokenService.generateToken(u.getId(),username,"xinlilaoshi","管理员");

        System.out.println("心理老师登陆token："+token);
        return R.ok().put("token", token);
    }

    /**
     * 注册
     */
    @IgnoreAuth
    @RequestMapping("/register")
    public R register(@RequestBody XinlilaoshiEntity xinlilaoshi){
        XinlilaoshiEntity u = xinlilaoshiService.selectOne(new EntityWrapper<XinlilaoshiEntity>().eq("laoshizhanghao", xinlilaoshi.getLaoshizhanghao()));
        if(u!=null) {
            return R.error("注册用户已存在");
        }
        Long uId = new Date().getTime();
        xinlilaoshi.setId(uId);
        xinlilaoshiService.insert(xinlilaoshi);
        return R.ok();
    }

    /*
    * 退出
    * */
    @RequestMapping("/logout")
    public R logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.ok();
    }

    /*
    * 获取用户的session用户信息
    * */
    @RequestMapping("/session")
    public R getCurrUser(HttpServletRequest request){
        Long id=(Long)request.getSession().getAttribute("userId");
        XinlilaoshiEntity u=xinlilaoshiService.selectById(id);
        return R.ok().put("data",u);
    }

    /*
    * 密码重置
    * */
    @RequestMapping(value = "resetPass")
    @IgnoreAuth
    public R resetPass(String username,HttpServletRequest request){
        XinlilaoshiEntity u=xinlilaoshiService.selectOne(new EntityWrapper<XinlilaoshiEntity>().eq("laoshizhanghao",username));
        if (u==null){
            return R.error("账号不存在");
        }
        u.setMima("123456");
        xinlilaoshiService.updateById(u);
        return R.ok("密码已重置为：123456");
    }

    /*
    * 后端列表
    * */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String , Object> params,XinlilaoshiEntity xinlilaoshi,
                  HttpServletRequest request){
        EntityWrapper <XinlilaoshiEntity> ew=new EntityWrapper<XinlilaoshiEntity>();
        PageUtils page=xinlilaoshiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xinlilaoshi), params), params));
        return R.ok().put("data", page);
    }

    /*
    * 前端列表
    * */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,XinlilaoshiEntity xinlilaoshi,
                  HttpServletRequest request){
        EntityWrapper<XinlilaoshiEntity> ew = new EntityWrapper<XinlilaoshiEntity>();
        System.out.println(xinlilaoshi);
        PageUtils page = xinlilaoshiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xinlilaoshi), params), params));
        return R.ok().put("data", page);
    }

    /*
    * 列表
    * */
    @RequestMapping("/lists")
    public R list( XinlilaoshiEntity xinlilaoshi){
        EntityWrapper<XinlilaoshiEntity> ew = new EntityWrapper<XinlilaoshiEntity>();
        ew.allEq(MPUtil.allEQMapPre( xinlilaoshi, "xinlilaoshi"));
        return R.ok().put("data", xinlilaoshiService.selectListView(ew));
    }

    /*
    * 查询
    * */
    @RequestMapping("/query")
    public R query(XinlilaoshiEntity xinlilaoshi){
        EntityWrapper< XinlilaoshiEntity> ew = new EntityWrapper< XinlilaoshiEntity>();
        ew.allEq(MPUtil.allEQMapPre( xinlilaoshi, "xinlilaoshi"));
        XinlilaoshiView xinlilaoshiView =  xinlilaoshiService.selectView(ew);
        return R.ok("查询心理老师成功").put("data", xinlilaoshiView);
    }

    /*
    *后端详情
    * */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        XinlilaoshiEntity xinlilaoshi = xinlilaoshiService.selectById(id);
        return R.ok().put("data", xinlilaoshi);
    }

    /*
    * 前端详情
    * */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        XinlilaoshiEntity xinlilaoshi = xinlilaoshiService.selectById(id);
        return R.ok().put("data", xinlilaoshi);
    }

    /*
    * 后端保存
    * */
    @RequestMapping("/save")
    public R save(@RequestBody XinlilaoshiEntity xinlilaoshi, HttpServletRequest request){
        xinlilaoshi.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(xinlilaoshi);
        XinlilaoshiEntity u = xinlilaoshiService.selectOne(new EntityWrapper<XinlilaoshiEntity>().eq("laoshizhanghao", xinlilaoshi.getLaoshizhanghao()));
        if(u!=null) {
            return R.error("用户已存在");
        }
        xinlilaoshi.setId(new Date().getTime());
        xinlilaoshiService.insert(xinlilaoshi);
        return R.ok();
    }

    /*
    * 前端保存
    * */
    @RequestMapping("/add")
    public R add(@RequestBody XinlilaoshiEntity xinlilaoshi, HttpServletRequest request){
        xinlilaoshi.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        XinlilaoshiEntity u=xinlilaoshiService.selectOne(new EntityWrapper<XinlilaoshiEntity>().eq("laoshizhanghao",xinlilaoshi.getLaoshizhanghao()));
        if(u!=null){
            return R.error("用户已存在");
        }
        xinlilaoshi.setId(new Date().getTime());
        xinlilaoshiService.insert(xinlilaoshi);
        return R.ok();
    }

    /*
    * 修改
    * */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody XinlilaoshiEntity xinlilaoshi,HttpServletRequest request){
        xinlilaoshiService.updateById(xinlilaoshi);
        return R.ok();
    }

    /*
    * 删除
    * */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        xinlilaoshiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
